package com.yunfeng.supercall.activity;

import java.util.Iterator;

import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;

import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

public class RegStep3Activity extends BaseActivity {

	private String mParams;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reg_step3);
		setTitleBar("输入密码");
		mParams = getIntent().getStringExtra("data");
		try {
			JSONObject json = new JSONObject(mParams);
			if (json.getInt("i_type") == 1) {
				setTextViewText(
						R.id.info,
						getString(R.string.has_been_super_number,
								new JSONObject(mParams).getString("i_phone")));
			} else {
				setTextViewText(
						R.id.info,
						getString(R.string.has_been_enterprise_super_number,
								new JSONObject(mParams).getString("i_phone")));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void next(View v) {
		String pass = getTextViewText(R.id.pass);
		String repass = getTextViewText(R.id.repass);
		// if (pass.length() < 8) {
		// showToast("密码由拼音+数字组成（需8位以上）");
		// return;
		// }
		// if (AppContext.isInvalidPassWord(pass)) {
		// showToast("密码由拼音+数字组成（需8位以上）");
		// return;
		// }
		if (TextUtils.isEmpty(pass)) {
			showToast("请输入密码");
			return;
		}
		if (!repass.equals(pass)) {
			showToast("两次密码输入不一致");
			return;
		}
		try {
			JSONObject json = new JSONObject(mParams);
			json.put("i_pass", getTextViewText(R.id.pass));
			AjaxParams mAjaxParams = new AjaxParams();
			Iterator<String> iterator = json.keys();
			while (iterator.hasNext()) {
				String key = iterator.next();
				mAjaxParams.put(key, json.get(key) + "");

			}
			showProgressDialog("正在注册...");
			mHttpClient.post("UserRegister", mAjaxParams, new MyAjaxCallBack() {

				@Override
				public void onReceiveError(String msg, String errorCode) {
					// TODO Auto-generated method stub
					showToast(msg);
					cancelProgressDialog();
				}

				@Override
				public void onReceiveData(String data, String msg) {
					// TODO Auto-generated method stub
					showToast("注册成功");
					cancelProgressDialog();
					startActivity(new Intent(RegStep3Activity.this,
							LoginActivity.class)
							.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
									| Intent.FLAG_ACTIVITY_CLEAR_TASK));
					finish();
				}
			});
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
